package com.songcha.module_category.ui.fragment.category_child

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseRefreshLoadMoreMultiViewModel
import com.songcha.library_base.mvvm.base.beanFlatMap
import com.songcha.library_business.bean.banner.BannerBean
import com.songcha.library_business.bean.book.BookListBean
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.module_category.bean.CategoryChannelSubListBean
import com.songcha.module_category.structs.CategorySubMultiItem
import com.songcha.module_category.structs.CategorySubMultiItem.Companion.CATEGORY_SUB_MULTI_ITEM_TYPE_END_BOOK
import com.songcha.module_category.structs.CategorySubMultiItem.Companion.CATEGORY_SUB_MULTI_ITEM_TYPE_TOP

class CategoryChildViewModel(app: Application):BaseRefreshLoadMoreMultiViewModel<CategoryChildRepository, CategorySubMultiItem>(app) {
    var channelId = 0
    var position = 0
   /* var categorySubList= MutableLiveData<MutableList<CategoryChannelSubListBean.ChannelSubBean>>()
    var categorySubEndBookList= MutableLiveData<MutableList<BookNetBean>>()*/
    val bannerList = MutableLiveData<MutableList<BannerBean.DataBean>>()

    fun getData(block:()->Unit){
        var subListBean:CategoryChannelSubListBean? = null

        val obs = repository!!.getCategoryChannelSubList(channelId)
            .beanFlatMap {
                subListBean = it
                repository!!.getCategoryChannelSubEndBookList(channelId,currentPage)
            }

        handleApiDataObserver(obs,object :
            IApiResult<BookListBean> {
            override fun onSuccess(bean: BookListBean) {
                if(subListBean==null) return
                val list = mutableListOf<CategorySubMultiItem>()
                val top = CategorySubMultiItem(CATEGORY_SUB_MULTI_ITEM_TYPE_TOP,subListBean!!.data)
                val end = getCastList(bean.data.records as MutableList<Any>)
                list.add(top)
                list.addAll(end)
                handleMultiDataListSuccess(list,bean.data.records as MutableList<Any>)

                block()
                //categorySubList.value=bean.data
            }

        },true)
    }

    fun getCategorySubEndBookList(){
        handleApiDataObserver(repository!!.getCategoryChannelSubEndBookList(channelId,currentPage),object :
            IApiResult<BookListBean> {
            override fun onSuccess(bean: BookListBean) {
                handleLoadMoreMultiDataListSuccess(bean.data.records as MutableList<Any>)
                //handleDataListSuccess2(bean.data.records as MutableList<Any>)
            }

        },true)
    }

    fun getBanner(){
        handleApiDataObserver(repository!!.getBanner(2),object :
            IApiResult<BannerBean> {
            override fun onSuccess(bean: BannerBean) {
                bannerList.value = bean.data
            }

        })
    }

    override fun getNotListCount(): Int {
        return 1
    }

    override fun getCastItem(item: Any): CategorySubMultiItem {
        return CategorySubMultiItem(CATEGORY_SUB_MULTI_ITEM_TYPE_END_BOOK,item as BookNetBean)
    }


}